<?php
namespace app\index\model;

use think\Model;

class Order extends Model
{
	public function Project(){
		return $this->hasOne('project','id','project_id')->field('project_name');
	}

    /*
     * 查询企业上传项目信息
     * @param $where where
     * @param $field field
     * @author cpy(1442435897@qq.com)
     */
    public function getInfo($where,$field = '*') {
        $res = $this->field($field)->where($where)->find();
        return empty($res)?array():$res->toArray();
    }

    /*
     * 查询所有企业用户项目
     * @param $where where
     * @param $field field
     * @author cpy(1442435897@qq.com)
     */
    public function selectInfo($where,$field = '*') {
        $res = $this->field($field)->where($where)->select();
        return empty($res)?array():$res->toArray();
    }

    /*
     * 查询所有企业用户项目
     * @param $where where
     * @param $field field
     * @author cpy(1442435897@qq.com)
     */
    public function selectGroupInfo($where,$group,$field = '*') {
        $res = $this->field($field)->where($where)->group($group)->select();
        return empty($res)?array():$res->toArray();
    }

    /*
     * 联合查询所有企业用户项目
     * @param $where where
     * @param $field field
     * @author cpy(1442435897@qq.com)
     */
    public function selectJoinGroupInfo($where,$group,$field = '*') {
        $res = $this->alias('a')->join('member b','b.id=a.uid')->field($field)->where($where)->group($group)->select();
        return empty($res)?array():$res->toArray();
    }

    /*
     * 查询所有企业用户项目
     * @param $where where
     * @param $field field
     * @author cpy(1442435897@qq.com)
     */
    public function selectLimitInfo($where,$limit,$field = '*') {
        $res = $this->field($field)->where($where)->limit($limit)->select();
        return empty($res)?array():$res->toArray();
    }


    /**
     * 查询所有项目投资记录
     * @param $where
     * @author hey_heyang(635870453@qq.com)
     */
    public function selectAllinvest() {
        $where['o.status'] = ['gt',1];
        $where['o.type'] = 1;
        $where['p.id'] = ['not in',[36,38,39,40]];
        $filed='sum(o.money) as money,p.project_name,p.picture';
        $res=$this->alias('o')
            ->join('project p','o.project_id=p.id')
            ->where($where)
            ->field($filed)
            ->group('o.project_id')
            ->order('p.id desc')
            ->select();
        return empty($res)?array():$res->toArray();
    }


    /**
     * 获取当日投资列表
     * @param $param
     * @author hey_heyang(635870453@qq.com)
     */
    public function  getTodayOrderList($param){
        if(!empty($param['time_begin']) && !empty($param['time_end'])){
            $start_time=strtotime($param['time_begin'].' 00:00:00');
            $end_time =strtotime($param['time_end'].' 23:59:59');
        }else{
            $start_time = strtotime('today');
            $end_time = strtotime('today')+86400;
        }

        $where['o.status'] = ['gt',1];
        $where['o.type'] = 1;
        $where['o.create_time'] = ['between',$start_time.','.$end_time];
        $where['p.id'] = ['not in',[36,38,39,40]];
        $filed='o.money,o.order_sn,p.project_name,m.username,m.mobile';
        $res=$this->alias('o')
            ->join('project p','o.project_id=p.id')
            ->join('member m','o.uid=m.id')
            ->where($where)
            ->field($filed)
            ->select();
        $res= empty($res)?array():$res->toArray();
        $total= $this->alias('o')
            ->join('project p','o.project_id=p.id')
            ->join('member m','o.uid=m.id')
            ->where($where)
            ->sum('o.money');
        $data['res'] = $res;
        $data['total'] = empty($total)?0:$total;
        return $data;
    }
}